import 'package:flutter_test/flutter_test.dart';
import 'package:uri_to_file/uri_to_file.dart';
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
import 'package:flutter/services.dart';
import 'dart:io';

class MockUriToFilePlatform
    with MockPlatformInterfaceMixin
    implements UriToFilePlatform {

  @override
  bool isUriSupported(String uriString) {
    return uriString.contains('content://');
  }

  @override
  Future<File> toFile(String uriString) async {
    return File('/mock/path/file.txt');
  }

  @override
  Future<void> clearTemporaryFiles() async {
    // Mock implementation
  }
}

void main() {
  TestWidgetsFlutterBinding.ensureInitialized();
  const MethodChannel channel = MethodChannel('in.lazymanstudios.uritofile/helper');
  
  // 设置mock方法调用处理器
  setUp(() {
    TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
        .setMockMethodCallHandler(
      channel,
      (MethodCall methodCall) async {
        if (methodCall.method == 'clearTemporaryFiles') {
          return null;
        } else if (methodCall.method == 'fromUri') {
          // 为fromUri方法返回一个模拟的文件路径字符串
          return '/mock/path/file.txt';
        }
        return null;
      },
    );
  });
  
  tearDown(() {
    TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
        .setMockMethodCallHandler(channel, null);
  });
  
  final UriToFilePlatform initialPlatform = UriToFilePlatform.instance;

  test('$MethodChannelUriToFile is the default instance', () {
    expect(initialPlatform, isInstanceOf<MethodChannelUriToFile>());
  });

  test('isUriSupported function exists', () {
    expect(() => isUriSupported('test'), returnsNormally);
  });

  test('toFile function exists', () async {
    // 对于异步函数，我们需要使用async和await
    expect(toFile('test'), completes);
  });

  test('clearTemporaryFiles function exists', () async {
    // 使用completes匹配器来验证异步函数是否正常完成
    expect(clearTemporaryFiles(), completes);
  });

  test('set instance and use mock implementation', () {
    final MockUriToFilePlatform fakePlatform = MockUriToFilePlatform();
    UriToFilePlatform.instance = fakePlatform;
    
    expect(UriToFilePlatform.instance, fakePlatform);
    expect(fakePlatform.isUriSupported('content://test'), isTrue);
    expect(fakePlatform.isUriSupported('file://test'), isFalse);
  });
}
